﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Imports DevExpress.XtraReports.UI

Public Class FormMain
    Private WithEvents UcDigitalKeyboard1 As ucDigitalKeyboard
    Dim objUser As New clsUser
    Dim objMstJL As New clsMstJL
    Dim objProdInfo As New clsProdInfo
    Dim objProdSpec As New clsProdSpec
    Dim objPgdMes As New clsPgdMes
    Dim objStorageLocationMst As New clsStorageLocationMst
    Dim objFLBinfo As New clsFLBinfo
    Dim objGJBinfo As New clsGJBinfo
    Dim objAutoNumber As New clsAutoNumber
    Dim dtPI As DataTable
    Dim dtPS As DataTable
    Dim dtPM As DataTable
    Const UNCHECKED As String = "未复核"
    Const CHECKED As String = "已复核"
    Dim warehousecode As String '仓库编码
    Dim objPgdMesWorkReport As New clsPgdMesWorkReport
    Dim objLhCacheArea As New clsLhCacheArea
    Dim httpHelper As New HttpHelper
    Dim dtjpd As New DataTable
    Dim rb_checked_text As String = "3,2"
    Dim rb_checked_details_text As String = "6"
    Private Sub FormMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dtjpd.Columns.Add("model")
        dtjpd.Columns.Add("material_code")
        connectDB()
        warehousecode = GetIniFile("WAREHOUSE", "warehouse_code", "", g_szInitFileName)
        RadioButton4.Checked = True
        cmbWorkReportMesType.SelectedIndex = 0
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_YZBHJ.ini"

        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))

        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If
    End Sub

    Private Sub getUserListByCJ()
        Dim dtUser As DataTable = objUser.getUserListByCJ(rb_checked_text, rb_checked_details_text)
        cmbOpers.Properties.Items.Clear()
        For index = 0 To dtUser.Rows.Count - 1
            cmbOpers.Properties.Items.Add(dtUser.Rows(index)("user_name"))
        Next
        cmbOpers.Properties.Items(0).CheckState = CheckState.Checked
        cmbOpers.Properties.DropDownRows = cmbOpers.Properties.Items.Count + 1
        cmbOpers.Properties.ShowButtons = False
        cmbOpers.Properties.ShowPopupCloseButton = False
    End Sub

    Private Sub getRbChechedText()
        If RadioButton4.Checked Then
            rb_checked_text = "3,2"
            rb_checked_details_text = "6"
        End If
        If RadioButton5.Checked Then
            rb_checked_text = "3,2"
            rb_checked_details_text = "8"
        End If
        If RadioButton6.Checked Then
            rb_checked_text = "3,2"
            rb_checked_details_text = "10"
        End If
        If RadioButton1.Checked Then
            rb_checked_text = "2"
            rb_checked_details_text = "1"
        End If
    End Sub

    Private Sub RadioButtonText_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged, RadioButton5.CheckedChanged, RadioButton6.CheckedChanged, RadioButton1.CheckedChanged
        getRbChechedText()
        getUserListByCJ()
    End Sub

    Private Sub txt_MouseDown(sender As Object, e As MouseEventArgs) Handles allFloors.MouseDown, jpFloors.MouseDown, gbFloors.MouseDown, txtBHJZL_SJ.MouseDown, txtBBPZZL_SJ.MouseDown, txtGQNCJL_SJ.MouseDown, txtTZBHJL_SJ.MouseDown
        UcDigitalKeyboard1 = New ucDigitalKeyboard
        If sender.MaskBox.Mask.EditMask = "d" Then
            UcDigitalKeyboard1.B_01 = False
        Else
            UcDigitalKeyboard1.B_01 = True
        End If

        If sender.GetType().Name.Equals("TextEdit") Then
            UcDigitalKeyboard1.B_02 = True
        Else
            UcDigitalKeyboard1.B_02 = False
        End If
        UcDigitalKeyboard1.B_02 = True
        UcDigitalKeyboard1.Txt = sender

        Me.Controls.Add(UcDigitalKeyboard1)
        UcDigitalKeyboard1.Show()
        UcDigitalKeyboard1.BringToFront()
        UcDigitalKeyboard1.Focus()
    End Sub

    Private Sub ucdigitalkeyboard1_leave(sender As Object, e As EventArgs) Handles UcDigitalKeyboard1.Leave
        UcDigitalKeyboard1.Dispose()
    End Sub

    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        Search()
    End Sub

    Private Sub Search()
        If String.IsNullOrEmpty(txtTP.Text.Trim()) = False Then
            dtPI = objProdInfo.getProdInfoListByTrayNoAndStatus(txtTP.Text.Trim(), "1")
            If dtPI.Rows.Count > 0 Then
                lblprodid.Text = dtPI.Rows(0)("prod_id").ToString()
                lblProdSpecId.Text = dtPI.Rows(0)("prod_spec_id").ToString()
                dtPS = objProdSpec.selectAllbySpecId(dtPI.Rows(0)("prod_spec_id").ToString())
                Dim dtjp As String = dtPS.Rows(0)("jp_material_code").ToString()
                Dim dtjps As String() = dtjp.Split("/")
                Dim dtjpp As String = dtPS.Rows(0)("jp_type").ToString()
                Dim dtjpps As String() = dtjpp.Split("/")
                dtjpd.Rows.Clear()
                For index = 0 To dtjps.Count() - 1
                    dtjpd.Rows.Add(dtjpps(index), dtjps(index))
                Next
                cmbJPXH_SJ.DisplayMember = "model"
                cmbJPXH_SJ.ValueMember = "material_code"
                cmbJPXH_SJ.DataSource = dtjpd
                If dtjpd.Rows.Count > 0 Then
                    If String.IsNullOrEmpty(dtPI.Rows(0)("jp_type").ToString()) Then
                        cmbJPXH_SJ.SelectedIndex = 0
                        lblJPXH_SJ.Text = cmbJPXH_SJ.Text.Trim()
                    Else
                        lblJPXH_SJ.Text = dtPI.Rows(0)("jp_type").ToString()
                        cmbJPXH_SJ.Text = lblJPXH_SJ.Text
                    End If
                End If
                dtPM = objPgdMes.getList(dtPI.Rows(0)("pgd_id").ToString())
                If dtPM.Rows.Count = 0 Then
                    dtPM = objPgdMes.getListBySysId(dtPI.Rows(0)("pgd_id").ToString())
                End If
                If dtPS.Rows.Count > 0 Then
                    '工艺设计信息
                    Me.lblBHJXH.Text = dtPS.Rows(0)("bhj_jp_type")
                    Me.lblJSTZ_WT.Text = dtPS.Rows(0)("jstz_wt")
                    Me.lblGJBXH.Text = dtPS.Rows(0)("gjb_type")
                    Me.lblJPZL.Text = dtPS.Rows(0)("rubber_layer_thickness_total")
                    Me.lblJP1ZL.Text = dtPS.Rows(0)("jp_st_wt")
                    Me.lblJP1ZLWC.Text = dtPS.Rows(0)("jp_st_wt_error_range")
                    Me.lblGJBZL.Text = dtPS.Rows(0)("gjb_st_wt")
                    Me.lblBHJZL.Text = dtPS.Rows(0)("bhj_st_wt")
                    Me.lblJPPS.Text = dtPS.Rows(0)("jp_layer_num")
                    Me.lblGJBPS.Text = dtPS.Rows(0)("gjb_layer_num")
                    Me.lblFLBXH.Text = dtPS.Rows(0)("flb_type")
                    Me.lblXJZZL.Text = dtPS.Rows(0)("xj_wt_total")
                    'Me.lbltotal_wt_error_range.Text = CDbl(dtPS.Rows(0)("total_wt_error_range"))

                    If lblGJBXH.Text.Contains("500") Or lblGJBXH.Text.Contains("600") Then
                        setLoc("A2", "A1")
                    ElseIf lblGJBXH.Text.Contains("700") Or lblGJBXH.Text.Contains("800") Then
                        setLoc("A4", "A3")
                    ElseIf lblGJBXH.Text.Contains("900") Or lblGJBXH.Text.Contains("1000") Then
                        setLoc("A6", "A5")
                    End If
                    '法兰板信息
                    Dim dtFLBinfo As DataTable = objFLBinfo.selectALlBySysId(dtPI.Rows(0)("flb_sys_id").ToString())
                    lblFLBXH_SJ.Text = dtFLBinfo.Rows(0)("flb_type")
                    lblwhich_side.Text = IIf(dtFLBinfo.Rows(0)("which_side") = 0, "下层法兰板", "上层法兰板")
                    lblflbNum.Text = CInt("1")
                    '骨架板信息
                    Dim dtGJBinfo As DataTable = objGJBinfo.getListBySysId(dtPI.Rows(0)("gjb_sys_id").ToString())
                    lblGJBPS_SJ.Text = dtGJBinfo.Rows(0)("layer_num")
                    lblGJBZL_SJ.Text = dtGJBinfo.Rows(0)("wt")
                    lblGJBXH_SJ.Text = dtGJBinfo.Rows(0)("gjb_type")
                    '胶片信息
                    lblJPPS_SJ.Text = dtPI.Rows(0)("jp_layer_num").ToString()
                    If lblJPPS_SJ.Text.Equals(lblJPPS.Text) = False Then
                        lblJPPS_SJ.ForeColor = Color.Red
                    End If
                    Dim ncjzzhl As String = (CDbl(dtPI.Rows(0)("jp1wt")) + CDbl(dtPI.Rows(0)("jp2wt")) + CDbl(dtPI.Rows(0)("jp3wt")) +
                                        CDbl(dtPI.Rows(0)("jp4wt")) + CDbl(dtPI.Rows(0)("jp5wt")) + CDbl(dtPI.Rows(0)("jp6wt")) +
                                        CDbl(dtPI.Rows(0)("jp7wt")) + CDbl(dtPI.Rows(0)("jp8wt")) + CDbl(dtPI.Rows(0)("jp9wt")) +
                                        CDbl(dtPI.Rows(0)("jp10wt")) + CDbl(dtPI.Rows(0)("jp11wt")) + CDbl(dtPI.Rows(0)("jp12wt")) +
                                        CDbl(dtPI.Rows(0)("jp13wt")) + CDbl(dtPI.Rows(0)("jp14wt")) + CDbl(dtPI.Rows(0)("jp15wt")) +
                                        CDbl(dtPI.Rows(0)("jp16wt")) + CDbl(dtPI.Rows(0)("jp17wt")) + CDbl(dtPI.Rows(0)("jp18wt")) +
                                        CDbl(dtPI.Rows(0)("jp19wt")) + CDbl(dtPI.Rows(0)("jp20wt")) + CDbl(dtPI.Rows(0)("jp21wt")) +
                                        CDbl(dtPI.Rows(0)("jp22wt")) + CDbl(dtPI.Rows(0)("jp23wt")) + CDbl(dtPI.Rows(0)("jp24wt")) +
                                        CDbl(dtPI.Rows(0)("jp25wt"))).ToString()
                    Dim ncjzzhl_t As Decimal = Decimal.Parse(ncjzzhl)
                    If ncjzzhl_t > 0 Then
                        lblJPZL_SJ.Text = ncjzzhl
                    Else
                        lblJPZL_SJ.Text = dtPI.Rows(0)("jp_all_wt")
                    End If

                    Dim jgz = Math.Abs(CDbl(lblJPZL_SJ.Text) - CInt(lblJPPS.Text) * CDbl(lblJP1ZL.Text))
                    Dim wcc = CInt(lblJPPS.Text) * CDbl(lblJP1ZLWC.Text)

                    If jgz <= wcc Then
                        lblOKNG.Text = "OK"
                    Else
                        lblOKNG.Text = "NG"
                    End If

                    '计算胶量重量
                    jsWT()
                End If
            Else
                MsgBox("未查询到预组装完成状态下的此托盘码", vbYes, "提示")
            End If
        Else
            MsgBox("请扫描或输入托盘码", vbYes, "提示")
        End If
    End Sub

    Private Sub txtTP_KeyDown(sender As Object, e As KeyEventArgs) Handles txtTP.KeyDown
        If e.KeyCode = Keys.Enter Then
            Search()
        End If
    End Sub

    Private Sub cmbJPXH_SJ_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbJPXH_SJ.SelectedIndexChanged
        lblJPXH_SJ.Text = cmbJPXH_SJ.Text.Trim()
    End Sub

    Private Sub setLoc(l_Ax As String, l_Ay As String)
        Dim dtSLM As DataTable = objStorageLocationMst.getAvailableList("yzzck", l_Ax, l_Ay)
        cmbLoc.Items.Clear()
        If dtSLM.Rows.Count > 0 Then
            For index = 0 To dtSLM.Rows.Count - 1
                cmbLoc.Items.Add(dtSLM.Rows(index)("location_no"))
            Next
            cmbLoc.SelectedIndex = 0
        End If
    End Sub

    Public Sub jsWT()
        lblTZGBJL_SJ.Text = FormatNumber((Decimal.Parse(lblGJBZL.Text) - Decimal.Parse(lblGJBZL_SJ.Text)) * 0.135, 3, vbTrue)
        lblSJZL.Text = FormatNumber(CDbl(lblXJZZL.Text) + CDbl(lblTZGBJL_SJ.Text) + Decimal.Parse(lblJSTZ_WT.Text), 3, vbTrue)
        lblSJZL_2.Text = FormatNumber(CDbl(lblJPZL_SJ.Text) + CDbl(txtBHJZL_SJ.Text), 3, vbTrue)
        lblXTZJL.Text = FormatNumber(CDbl(lblSJZL.Text) - CDbl(lblSJZL_2.Text), 3, vbTrue)
        lblTZNCJZL.Text = FormatNumber(CDbl(lblXTZJL.Text) - CDbl(txtTZBHJL_SJ.Text), 3, vbTrue)
        If CDbl(lblTZNCJZL.Text) < 0 Then
            txtGQNCJL_SJ.Value = -(CDbl(lblTZNCJZL.Text))
            txtBBPZZL_SJ.Value = 0.00
        End If

        If CDbl(lblTZNCJZL.Text) > 0 Then
            txtBBPZZL_SJ.Value = CDbl(lblTZNCJZL.Text)
            txtGQNCJL_SJ.Value = 0.00
        End If

        'If Math.Abs(CDbl(lblSJZL.Text) - CDbl(lblSJZL_2.Text)) <= CDbl(lbltotal_wt_error_range.Text) Then
        '    lblOKNG.Text = "OK"
        'Else
        '    lblOKNG.Text = "NG"
        'End If
    End Sub

    Private Sub txtTZBHJL_SJ_TextChanged(sender As Object, e As EventArgs) Handles txtTZBHJL_SJ.TextChanged
        jsWT()
    End Sub

    Private Sub txtJSTZJL_SJ_TextChanged(sender As Object, e As EventArgs)
        jsWT()
    End Sub

    Private Sub txtBHJZL_SJ_TextChanged(sender As Object, e As EventArgs) Handles txtBHJZL_SJ.TextChanged
        jsWT()
    End Sub
    Private Function check() As Boolean
        Dim rtn As Boolean = True
        If String.IsNullOrEmpty(cmbOpers.EditValue.ToString()) Then
            MsgBox("请选择操作用户！", vbYes, "提示")
            Return False
        End If
        '托盘码
        If String.IsNullOrEmpty(txtTP.Text.Trim()) Then
            MsgBox("请扫描或输入托盘码", vbYes, "提示")
            Return False
        End If
        '胶料复核
        'If jpModelCheckButton.Checked = False Then
        '    MsgBox("请复核胶料信息", vbYes, "提示")
        '    Return False
        'End If
        '总层数复核
        If allFloors.Value <= 0 Then
            MsgBox("请填写复核总层数", vbYes, "提示")
            Return False
        End If
        If allFloorsCheckButton.Checked = False Then
            MsgBox("请复核总层数信息", vbYes, "提示")
            Return False
        End If

        '钢板层数复核
        If gbFloors.Value <= 0 Then
            MsgBox("请填写复核钢板层数", vbYes, "提示")
            Return False
        End If
        If gbCheckButton.Checked = False Then
            MsgBox("请复核钢板层数信息", vbYes, "提示")
            Return False
        End If

        '胶片层数复核
        If jpFloors.Value <= 0 Then
            MsgBox("请填写复核胶片层数", vbYes, "提示")
            Return False
        End If
        If jpCheckButton.Checked = False Then
            MsgBox("请复核胶片层数信息", vbYes, "提示")
            Return False
        End If

        '总层数复核
        If CInt(allFloors.Text) <> (CInt(lblGJBPS.Text) + CInt(lblJPPS.Text)) Then
            MsgBox("总层数与设计不符，请确认是否正确。", vbYes, "提示")
            Return False
        End If

        '胶片层数复核
        If CInt(jpFloors.Text) <> CInt(lblJPPS.Text) Then
            MsgBox("胶片层数与设计不符，请确认是否正确。", vbYes, "提示")
            Return False
        End If

        '钢板层数复核
        If CInt(gbFloors.Text) <> CInt(lblGJBPS.Text) Then
            MsgBox("钢板层数与设计不符，请确认是否正确。", vbYes, "提示")
            Return False
        End If

        '总层数复核未复核 复核
        If allFloorsCheckButton.Checked = False Then
            MsgBox("总层数复核未复核，请复核。", vbYes, "提示")
            Return False
        End If

        '钢板层数复核未复核 复核
        If gbCheckButton.Checked = False Then
            MsgBox("钢板层数复核未复核，请复核。", vbYes, "提示")
            Return False
        End If

        '胶片层数复核未复核 复核
        If jpCheckButton.Checked = False Then
            MsgBox("胶片层数复核未复核，请复核。", vbYes, "提示")
            Return False
        End If

        '保护胶 批次号
        If String.IsNullOrEmpty(txtBHJ_PC.Text.Trim()) Then
            MsgBox("请扫描或输入保护胶批次号。", vbYes, "提示")
            Return False
        End If
        '保护胶为0时，不能保存
        If txtBHJZL_SJ.Value <= 0 Then
            MsgBox("请输入保护胶重量。", vbYes, "提示")
            Return False
        End If

        If String.IsNullOrEmpty(lblOKNG.Text.Trim()) Then
            MsgBox("未获取到判定结果！请检查。", vbYes, "提示")
            Return False
        End If

        'If lblOKNG.Text.Equals("NG") Then
        '    MsgBox("判定结果为NG！请检查。", vbYes, "提示")
        '    Return False
        'End If

        Return rtn
    End Function

    Private Sub allFloorsCheckButton_CheckedChanged(sender As Object, e As EventArgs) Handles allFloorsCheckButton.CheckedChanged
        If sender.Checked = True Then
            sender.Text = CHECKED
        Else
            sender.Text = UNCHECKED
        End If
    End Sub

    Private Sub jpCheckButton_CheckedChanged(sender As Object, e As EventArgs) Handles jpCheckButton.CheckedChanged
        If sender.Checked = True Then
            sender.Text = CHECKED
        Else
            sender.Text = UNCHECKED
        End If
    End Sub

    Private Sub jpModelCheckButton_CheckedChanged(sender As Object, e As EventArgs) Handles jpModelCheckButton.CheckedChanged
        If sender.Checked = True Then
            sender.Text = CHECKED
        Else
            sender.Text = UNCHECKED
        End If
    End Sub

    Private Sub gbCheckButton_CheckedChanged(sender As Object, e As EventArgs) Handles gbCheckButton.CheckedChanged
        If sender.Checked = True Then
            sender.Text = CHECKED
        Else
            sender.Text = UNCHECKED
        End If
    End Sub

    Private Sub txtBHJ_PC_KeyDown(sender As Object, e As KeyEventArgs) Handles txtBHJ_PC.KeyDown
        If e.KeyCode = Keys.Enter Then
            If String.IsNullOrEmpty(txtBHJ_PC.Text.Trim()) = False Then
                'txtBHJ_PC.ImeMode = ImeMode.Off
                Dim barcode As String = txtBHJ_PC.Text.Trim()
                'Dim barcode_byte As Byte() = System.Text.Encoding.UTF8.GetBytes(barcode)
                'Dim barcode_string As String = System.Text.Encoding.UTF8.GetString(barcode_byte)
                'txtBHJ_PC.Text = barcode_string
                txtBHJ_PC.Text = barcode.Replace("???", "车")
            Else
                MsgBox("请扫描或输入保护胶批次号。", vbYes, "提示")
            End If
        End If
    End Sub

    Private Sub setProdInfo(ByRef prodInfo As entity_ProdInfo)
        Dim checkAll As String
        Dim checkJP As String
        Dim checkGB As String
        If allFloorsCheckButton.Checked = True Then
            checkAll = "1"
        Else
            checkAll = "0"
        End If
        If jpCheckButton.Checked = True Then
            checkJP = "1"
        Else
            checkJP = "0"
        End If
        If gbCheckButton.Checked = True Then
            checkGB = "1"
        Else
            checkGB = "0"
        End If
        With prodInfo
            ' 支座成品物料编码
            .prod_spec_id = lblProdSpecId.Text
            ' 胶料型号
            ' .jp_type = lblJPXH_SJ.Text
            .jp_type = cmbJPXH_SJ.Text
            ' 胶片总重量
            .jp_all_wt = lblJPZL_SJ.Text
            ' 库位号
            .location = cmbLoc.Text
            ' 入库人员
            .in_st_ope = cmbOpers.EditValue.ToString()
            '保护胶 批次号
            .bhj_pcno = txtBHJ_PC.Text.Trim()
            ' 保护胶
            .bhj_wt = txtBHJZL_SJ.Text
            ' 技术调整
            .jstz_wt = lblJSTZ_WT.Text
            ' 补薄片
            .bbp_wt = txtBBPZZL_SJ.Text
            ' 调整钢板
            .tzgb_wt = lblTZGBJL_SJ.Text
            ' 割去内层胶
            .gqncj_wt = txtGQNCJL_SJ.Text
            ' 实际割去内层胶
            If rbYes.Checked Then
                .is_gqncj = rbYes.Text
            End If
            If rbNo.Checked Then
                .is_gqncj = rbNo.Text
            End If
            ' 调整保护胶量
            .tzbhjl_wt = txtTZBHJL_SJ.Text
            ' 割去保护胶
            '.gqbhj_wt = lblgqbhjlL_SJ.Text
            ' 复合班员
            .rv_ope = cmbOpers.EditValue.ToString()
            ' 合模人员
            .rv_mould_ope = cmbOpers.EditValue.ToString()
            ' 总层数
            .rv_layer_num = allFloors.Text
            ' 总层数复核结果
            .rv_layer_okng = checkAll
            ' 总层数复核员
            .rv_layer_ope = cmbOpers.EditValue.ToString()
            ' 胶片层数
            .rv_jp_layer_num = jpFloors.Text
            ' 胶片层数复核结果
            .rv_jp_layer_okng = checkJP
            ' 胶片层数复核员
            .rv_jp_layer_ope = cmbOpers.EditValue.ToString()
            ' 钢板层数
            .rv_gb_layer_num = gbFloors.Text
            ' 钢板层数复核结果
            .rv_gb_layer_okng = checkGB
            ' 钢板层数复核员
            .rv_gb_layer_ope = cmbOpers.EditValue.ToString()
            ' 设定保护胶操作员
            .set_bhj_ope = cmbOpers.EditValue.ToString()
        End With
    End Sub

    Private Sub btnFinish_Click(sender As Object, e As EventArgs) Handles btnFinish.Click
        'Dim rtn_mes_bg As String = mesbg("1", dtPM.Rows(0)("type_value"), dtPM.Rows(0)("mes_project_code"))
        'Dim jo As JObject = JsonConvert.DeserializeObject(rtn_mes_bg)
        'Dim code As String = jo("code").ToString() '输出0
        'Dim msg As String = jo("msg").ToString() '输出操作成功
        'mesRk("1", warehousecode, "02.02.023", "GⅡ型700支座-结构七-S-CL-001")
        If check() Then
            If MsgBox("确认要进行完工吗？", vbYesNo, "提示") = vbYes Then
                If lblOKNG.Text.Equals("NG") Then
                    If MsgBox("判定结果为NG！确认要完工吗？", vbYesNo, "提示") = vbYes Then
                        makeWG()
                    End If
                Else
                    makeWG()
                End If
            End If
        End If
    End Sub

    Private Sub makeWG()
        btnFinish.Enabled = False
        Dim prodInfo As New entity_ProdInfo
        setProdInfo(prodInfo)
        Dim dtMstJL As DataTable = objMstJL.getProdInfoListByMstTypeAndMstValue("胶片", prodInfo.jp_type)
        Dim curWeightJp = CDbl(dtMstJL.Rows(0)("cur_weight")) - CDbl(prodInfo.jp_all_wt)
        If curWeightJp < 0 Then
            MsgBox("该支座胶片库存不足，无法进行完工", vbYes, "提示")
            btnFinish.Enabled = True
            Exit Sub
        End If
        Dim dtMstBHJ As DataTable = objMstJL.getProdInfoListByMstType("保护胶")
        Dim dttMstBHJ = CDbl(dtMstBHJ.Rows(0)("cur_weight"))
        Dim bhj_w As String = Decimal.Parse(prodInfo.bhj_wt) + Decimal.Parse(prodInfo.tzbhjl_wt)
        Dim curWeightBHJ = dttMstBHJ - bhj_w
        If curWeightBHJ < 0 Then
            MsgBox("该支座保护胶库存不足，无法进行完工", vbYes, "提示")
            btnFinish.Enabled = True
            Exit Sub
        End If
        Dim b_SysId As String = dtPI.Rows(0)("b_pgd_yzz_details_sys_id").ToString()
        Dim code_jlck As String = Now.ToString("yyyyMMdd") & "-" & objAutoNumber.getNextKeyNo("jl_ck_code", "jlckAutoNumber")
        Dim rtn As Integer = objProdInfo.updateProdInfo(lblprodid.Text.Trim(), prodInfo, txtTP.Text.ToUpper().Trim(), code_jlck, b_SysId, dtPM.Rows(0)("material_code"), cmbJPXH_SJ.Text.Trim())
        If rtn <> -2 Then
            Try
                objAutoNumber.setNextKeyNo("jl_ck_code", "jlckAutoNumber")
            Catch ex As Exception
                MsgBox("设置胶料出库单号异常：" & ex.Message.ToString(), vbYes, "提示")
            End Try
            barcodePrint() '标签打印
            Try
                If cmbWorkReportMesType.Text.Trim().Equals("客户端MES报工入库") Then
                    Dim rtn_mes_bg As String = mesbg("1", dtPM.Rows(0)("type_value"), dtPM.Rows(0)("mes_project_code"))
                    Dim jo_bg As JObject = JsonConvert.DeserializeObject(rtn_mes_bg)
                    Dim code_bg As String = jo_bg("code").ToString() '输出0
                    Dim msg_bg As String = jo_bg("msg").ToString() '输出操作成功
                    Dim rnt_mes_rk As String = mesRk("1", warehousecode, dtPM.Rows(0)("type_value"), dtPM.Rows(0)("mes_project_code"))
                    Dim jo_rk As JObject = JsonConvert.DeserializeObject(rnt_mes_rk)
                    Dim code_rk As String = jo_rk("code").ToString() '输出0
                    Dim msg_rk As String = jo_rk("msg").ToString() '输出操作成功
                    objPgdMesWorkReport.insert("ok", b_SysId, "1", cmbOpers.EditValue.ToString(), lblprodid.Text.Trim(), 0, code_bg)
                    objLhCacheArea.update(lblprodid.Text.Trim(), "0", code_rk)
                    If code_bg.Equals("0") Then
                        If code_rk.Equals("0") Then
                            MsgBox("完工成功，MES报工成功，MES入库成功", vbYes, "提示")
                        Else
                            MsgBox("完工成功，MES报工成功，MES入库失败：" & msg_rk, vbYes, "提示")
                        End If
                    Else
                        If code_rk.Equals("0") Then
                            MsgBox("完工成功，MES报工失败：" & msg_bg & "，MES入库成功", vbYes, "提示")
                        Else
                            MsgBox("完工成功，MES报工失败：" & msg_bg & "，MES入库失败：" & msg_rk, vbYes, "提示")
                        End If
                    End If
                End If
                If cmbWorkReportMesType.Text.Trim().Equals("服务器MES报工入库") Then
                    objPgdMesWorkReport.insert(b_SysId, cmbOpers.EditValue.ToString(), lblprodid.Text.Trim())
                    MsgBox("完工成功", vbYes, "提示")
                End If
                Clear() '清空
            Catch ex As Exception
                MsgBox("MES报工入库异常：" & ex.Message.ToString(), vbYes, "提示")
            End Try
        Else
            MsgBox("完工失败", vbYes, "提示")
        End If
        btnFinish.Enabled = True
    End Sub
    Private Sub Clear()
        txtTP.Text = String.Empty
        lblprodid.Text = String.Empty
        lblProdSpecId.Text = String.Empty
        lblFLBXH.Text = "0"
        lblBHJXH.Text = "0"
        lblGJBXH.Text = "0"
        lblBHJZL.Text = "0"
        lblGJBPS.Text = "0"
        lblJSTZ_WT.Text = "0"
        lblGJBZL.Text = "0"
        lblJPPS.Text = "0"
        lblXJZZL.Text = "0"
        lblJPZL.Text = "0"
        lblJP1ZL.Text = "0"
        lblJP1ZLWC.Text = "0"
        lblFLBXH_SJ.Text = String.Empty
        lblflbNum.Text = "0"
        lblwhich_side.Text = String.Empty
        lblGJBXH_SJ.Text = String.Empty
        lblGJBPS_SJ.Text = "0"
        lblGJBZL_SJ.Text = "0.0"
        lblJPXH_SJ.Text = String.Empty
        lblJPPS_SJ.Text = "0"
        lblJPZL_SJ.Text = "0.0"
        allFloors.Text = "0"
        allFloorsCheckButton.Text = UNCHECKED
        gbFloors.Text = "0"
        gbCheckButton.Text = UNCHECKED
        jpFloors.Text = "0"
        jpCheckButton.Text = UNCHECKED
        txtBHJZL_SJ.Text = "0"
        txtTZBHJL_SJ.Text = "0.00"
        txtBBPZZL_SJ.Text = "0"
        txtGQNCJL_SJ.Text = "0"
        lblTZGBJL_SJ.Text = "0.00"
        lblSJZL.Text = "0.00"
        lblSJZL_2.Text = "0.00"
        lblXTZJL.Text = "0.00"
        lblTZNCJZL.Text = "0.00"
        lblOKNG.Text = String.Empty
        'lbltotal_wt_error_range.Text = "0"
        cmbLoc.Items.Clear()
    End Sub

    Private Sub btnMesWorkReportList_Click(sender As Object, e As EventArgs) Handles btnMesWorkReportList.Click
        Dim formMesWorkReportList As New FormMesWorkReportList()
        formMesWorkReportList.ShowDialog(Me)
    End Sub

    Private Function mesRk(oper_num As String, warehousecode As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """rkcount"":""" & Double.Parse(oper_num) & """," '入库数量
        params &= """warehousecode"":""" & warehousecode & """," '入库仓库编码
        params &= """productorname"":""" & productorname & """," '产品编码
        params &= """procedurename"":""" & "预组装" & """," '执行工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """departmentid"":""TS1103105""" '部门id
        params &= "}"
        Return httpHelper.HttpPostWebService("productFinishedToWarehouse", params)
    End Function

    Private Sub barcodePrint()
        Dim report As New XtraReport3()
        Dim printTool As New ReportPrintTool(report)
        Try
            If dtPI.Rows.Count > 0 Then
                If dtPS.Rows.Count > 0 Then
                    If dtPM.Rows.Count > 0 Then
                        report.Xr_lbl_support_model.Text = dtPS.Rows(0)("prod_spec_name")
                        report.Xr_lbl_project_name.Text = dtPM.Rows(0)("mes_project_name")
                        report.Xr_lbl_project_code.Text = dtPI.Rows(0)("project_no") 'dtxxx.Rows(0)("mes_project_code") '项目编号
                        report.Xr_lbl_yzz_DateTime.Text = Now.ToString 'dtx.Rows(0)("org_time") '预组装时间
                        report.XrBarCode1.Text = dtPI.Rows(0)("prod_id") 'lblprodid.Text.Trim() '生产编号
                        report.Xr_lbl_jp_type.Text = lblJPXH_SJ.Text.Trim() 'dtPI.Rows(0)("jp_type") 'lblJLXH.Text.Trim() '胶料类型     
                        report.Xr_lbl_gyh.Text = dtPI.Rows(0)("steel_seal_no") ' dtx.Rows(0)("steel_seal_no") '钢印号
                        report.Xr_lbl_location.Text = cmbLoc.Text '库位编号
                        printTool.Print()
                    End If
                End If
            End If
        Catch ex As Exception
            MsgBox("标签打印异常：" & ex.Message.ToString(), vbYes, "提示")
        End Try
    End Sub

    Private Function mesbg(oper_num As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """qualifiednumber"":""" & Double.Parse(oper_num) & """," '报工的合格数
        params &= """unqualifiednumber"":""" & 0.00 & """," '报工的不合格数
        params &= """productorname"":""" & productorname & """," '报工产品编码
        params &= """procedurename"":""" & "预组装" & """," '报工工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """unqualifiedReason"":""" & "" & """" '不合格原因
        params &= "}"
        Return httpHelper.HttpPostWebService("dispatchingOrderBG", params)
    End Function

    Private Sub btnJPZLRecord_Click(sender As Object, e As EventArgs) Handles btnJPZLRecord.Click
        Dim formJPZLRecord As New FormJPZLRecord()
        formJPZLRecord.ShowDialog(Me)
    End Sub

    Private Sub btnBHJRecord_Click(sender As Object, e As EventArgs) Handles btnBHJRecord.Click
        Dim formBHJRecord As New FormBHJRecord()
        formBHJRecord.ShowDialog(Me)
    End Sub

    Private Sub btnPrintBarCode_Click(sender As Object, e As EventArgs) Handles btnPrintBarCode.Click
        Dim formBarCodePrint As New FormBarCodePrint()
        formBarCodePrint.ShowDialog(Me)
    End Sub

    Private Sub FormMain_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

End Class

Public Class entity_ProdInfo
    ' 支座成品物料编码
    Public prod_spec_id As String
    ' 胶料类型
    Public jp_type As String
    ' 胶片总重量
    Public jp_all_wt As String
    ' 库位号
    Public location As String
    ' 入库人员
    Public in_st_ope As String
    ' 复合班员
    Public rv_ope As String
    ' 合模人员
    Public rv_mould_ope As String
    ' 总层数
    Public rv_layer_num As String
    ' 总层数复核结果
    Public rv_layer_okng As String
    ' 总层数复核员
    Public rv_layer_ope As String
    ' 胶片层数
    Public rv_jp_layer_num As String
    ' 胶片层数复核结果
    Public rv_jp_layer_okng As String
    ' 胶片层数复核员
    Public rv_jp_layer_ope As String
    ' 钢板层数
    Public rv_gb_layer_num As String
    ' 钢板层数复核结果
    Public rv_gb_layer_okng As String
    ' 钢板层数复核员
    Public rv_gb_layer_ope As String
    ' 保护胶作业员
    Public set_bhj_ope As String
    ' 保护胶 批次号
    Public bhj_pcno As String
    ' 保护胶
    Public bhj_wt As String
    ' 技术调整
    Public jstz_wt As String
    ' 调整钢板
    Public tzgb_wt As String
    ' 调整保护胶量
    Public tzbhjl_wt As String
    ' 补薄片
    Public bbp_wt As String
    ' 割去内层胶
    Public gqncj_wt As String
    ' 实际割去内层胶
    Public is_gqncj As String
    ' 割去保护胶
    'Public gqbhj_wt As String
    '法兰板厚度
    Public flb_H As String
End Class